using System;
using System.Collections.Generic;
using System.Text;
using System.CodeDom;
using System.CodeDom.Compiler;
using System.IO;

namespace Destra.Tools.DACGenerator.CodeGenerators
{
	public class CodeDomGenerator
	{
		private CodeDomProvider m_codeDomProvider;

        public static string GenerationComment = string.Format(@"Compiled with DACGenerator {0}, by {1}\{2} on {3}", System.Reflection.Assembly.GetExecutingAssembly().GetName().Version.ToString(), Environment.UserDomainName, Environment.UserName, DateTime.Now);

		public CodeDomGenerator(CodeDomProvider codeDomProvider)
		{
			m_codeDomProvider = codeDomProvider;
		}

		protected CodeDomProvider CodeDomProvider
		{
			get
			{
				return m_codeDomProvider;
			}
		}

		protected string GenerateCodeFromNamespace(CodeNamespace codeNamespace)
		{
			using (StringWriter writer = new StringWriter())
			{
				CodeGeneratorOptions options = new CodeGeneratorOptions();
				options.BracingStyle = "C";

                codeNamespace.Comments.Add(new CodeCommentStatement(GenerationComment));
				this.CodeDomProvider.GenerateCodeFromNamespace(codeNamespace, writer, options);

				return writer.ToString();
			}
		}
	}
}
